home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / dialog / field.c < prev    next >
C/C++ Source or Header  |  1996-08-01  |  8KB  |  389 lines

  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include "diadef.h"
  5. #include "dialog.h"
  6.  
  7. PUBLIC FIELD::FIELD(const char *_prompt)
  8. {
  9.     readonly = 0;
  10.     prompt = strdup(_prompt);
  11.     box.width = 30;        // Default visible width
  12. }
  13.  
  14.  
  15. PUBLIC VIRTUAL FIELD::~FIELD()
  16. {
  17.     free (prompt);
  18. }
  19. /*
  20.     Is the field editable or is it write protect.
  21.     Title field do use this feature.
  22. */
  23. PUBLIC int FIELD::is_readonly()
  24. {
  25.     return readonly;
  26. }
  27. /*
  28.     Set the readonly status of a field.
  29. */
  30. PUBLIC void FIELD::set_readonly()
  31. {
  32.     readonly = 1;
  33. }
  34.  
  35. /*
  36.     Process conditionnally a message and conditionnally redraw
  37.     yourself if needed. See radio.c for the intended application.
  38. */
  39. PROTECTED VIRTUAL void FIELD::processmsg(WINDOW *, FIELD_MSG &, int)
  40. {
  41. }
  42. /*
  43.     Get the width of the first column of a field.
  44.     This is only meaningful for a Menu item.
  45. */      
  46. PROTECTED VIRTUAL int FIELD::getwidth1 ()
  47. {
  48.     return 0;
  49. }
  50. /*
  51.     Set the width of the first column of a field.
  52.     This is only meaningful for a Menu item.
  53. */      
  54. PROTECTED VIRTUAL void FIELD::setwidth1 (int)
  55. {
  56. }
  57. /*
  58.     Return the second string of a menu item
  59. */
  60. PUBLIC VIRTUAL const char *FIELD::getmenustr()
  61. {
  62.     return NULL;
  63. }
  64.  
  65. /*
  66.     Build a key that uniquely identify this field in the dialog
  67. */
  68. PUBLIC VIRTUAL void FIELD::format_htmlkey(char *key, int nof)
  69. {
  70.     html_formatkey (key,"%s-%d",prompt,nof);
  71. }
  72.  
  73.  
  74. PRIVATE void FIELD_STRING_BASE::init (int maxsiz)
  75. {
  76.     x.input = x.scroll = 0;
  77.     password_mode = 0;
  78.     size = maxsiz;
  79.     buf = (char*)malloc_err (size+1);
  80. }
  81.  
  82. PROTECTED FIELD_STRING_BASE::FIELD_STRING_BASE(
  83.     const char *_prompt,
  84.     const char *_str,
  85.     int maxsiz)
  86.     : FIELD (_prompt)
  87. {
  88.     init(maxsiz);
  89.     strncpy (buf,_str,maxsiz);
  90.     buf[maxsiz] = '\0';
  91. }
  92.  
  93. PROTECTED FIELD_STRING_BASE::FIELD_STRING_BASE(
  94.     const char *_prompt,
  95.     int maxsiz)
  96.     : FIELD (_prompt)
  97. {
  98.     init(maxsiz);
  99.     buf[0] = '\0';
  100. }
  101. PUBLIC FIELD_STRING_BASE::~FIELD_STRING_BASE()
  102. {
  103.     free (buf);
  104. }
  105.  
  106. PUBLIC FIELD_STRING::FIELD_STRING(
  107.     const char *_prompt,
  108.     char *_str,
  109.     int maxsiz)
  110.     : FIELD_STRING_BASE (_prompt, _str, maxsiz)
  111. {
  112.     str = _str;
  113.     backup = strdup(_str);
  114. }
  115.  
  116. PUBLIC FIELD_STRING::~FIELD_STRING()
  117. {
  118.     free (backup);
  119. }
  120.  
  121. PUBLIC FIELD_PASSWORD::FIELD_PASSWORD(
  122.     const char *_prompt,
  123.     SSTRING &_str)
  124.     : FIELD_SSTRING (_prompt,_str)
  125. {
  126.     buf[0] = '\0';
  127.     password_mode = 1;
  128. }
  129.  
  130. PUBLIC void FIELD_STRING::save()
  131. {
  132.     strcpy (str,buf);
  133.     strip_end (str);
  134. }
  135.  
  136. PUBLIC void FIELD_STRING::restore()
  137. {
  138.     strcpy (str,backup);
  139. }
  140.  
  141. /*
  142.     Draw only the input part of a field
  143. */
  144. PUBLIC void FIELD_STRING_BASE::drawtxt (WINDOW *dialog)
  145. {
  146.     int blank_start;
  147.     wattrset(dialog, inputbox_attr);
  148.     wmove(dialog, box.y,box.x);
  149.     if (password_mode){
  150.         blank_start = 0;
  151.     }else{
  152.         // Make sure the string is not too long
  153.         char *instr = buf + x.scroll;
  154.         char *last_visible = instr + box.width;
  155.         char tmp = *last_visible;
  156.  
  157.         *last_visible = '\0';
  158.         waddstr (dialog,instr);
  159.         blank_start = strlen(instr);
  160.         *last_visible = tmp;
  161.     }
  162.     const int box_width = box.width;
  163.     for (int i=blank_start; i<box_width; i++) waddch (dialog,' ');
  164. }
  165.  
  166. /*
  167.     Draw the field with the prompt
  168. */
  169. PUBLIC void FIELD_STRING_BASE::html_draw(int nof)
  170. {
  171.     char key[100];
  172.     format_htmlkey (key,nof);
  173.     html_printf ("<tr><td>%s<td>",prompt);
  174.     html_defvar (password_mode ? "password" : "text",key,buf
  175.         ,"size=30 maxlength=256");
  176.     html_defvarcur (key,buf);
  177. }
  178.  
  179. /*
  180.     Grab the input of the user received from the www browser.
  181.     Validate that the Draw the field with the prompt
  182.     and compare the old value with the content of the buffer. If there
  183.     is a mismatch, it means this www form was too old and the input
  184.     is refused.
  185. */
  186. PUBLIC int FIELD_STRING_BASE::html_validate(int nof)
  187. {
  188.     int ret = -1;
  189.     char key[100];
  190.     format_htmlkey (key,nof);
  191.     const char *old_val = html_getoldval (key);
  192.     const char *new_val = html_getval (key);
  193. printf ("validate %s val :%s: old :%s: buf :%s:\n",key,new_val,old_val,buf);
  194. fflush (stdout);
  195.     if (strcmp(buf,old_val)==0){
  196.         strcpy (buf,new_val);
  197.         ret = 0;
  198.     }
  199.     return ret;
  200. }
  201.  
  202.  
  203.  
  204. /*
  205.     Draw the field with the prompt and the surrounding box
  206. */
  207. PUBLIC void FIELD::draw (WINDOW *dialog)
  208. {
  209.     int posy = box.y;
  210.     wmove(dialog, posy,1);
  211.     wattrset(dialog, dialog_attr);
  212.     waddstr (dialog,prompt);
  213.     int len = strlen(prompt);
  214.     int lastx = box.x-2;
  215.     for ( ; len < lastx; len++) waddch (dialog,' ');
  216.     drawtxt(dialog);
  217. }
  218.  
  219. /*
  220.     Position the cursor in the field
  221. */
  222. PUBLIC VIRTUAL void FIELD::setcursor(WINDOW *dialog)
  223. {
  224.     wmove (dialog,box.y,box.x);
  225. }
  226. /*
  227.     Called when the field loose selection. This was
  228.     done mostly for menu entries which is a very special case.
  229.     The default behavior does nothing.
  230. */
  231. PUBLIC VIRTUAL void FIELD::unselect(WINDOW *)
  232. {
  233. }
  234. PUBLIC void FIELD_STRING_BASE::setcursor(WINDOW *dialog)
  235. {
  236.     wmove (dialog,box.y,box.x+x.input-x.scroll);
  237. }
  238.  
  239. PUBLIC void FIELD_STRING_BASE::dokey (
  240.     WINDOW *dialog,
  241.     int key,
  242.     FIELD_MSG &)
  243. {
  244.     if (readonly) return;
  245.     int input_x = x.input;
  246.     int scroll = x.scroll;
  247.     const int box_width = box.width;
  248.     const int box_y = box.y;
  249.     const int box_x = box.x;
  250.     char *instr = buf;
  251.     int curlen = strlen(instr);
  252.     int last_scroll = scroll;
  253.     int mustdraw = 0;
  254.     switch (key){
  255.     case 1:        /* ^A like emacs */
  256.     case KEY_HOME:
  257.         input_x = 0;
  258.         scroll = 0;
  259.         break;
  260.     case 5:        /* ^E like Emacs */
  261.     case KEY_END:
  262.         input_x = curlen;
  263.         if (input_x > box_width){
  264.             scroll = input_x - box_width + 1;
  265.         }else{
  266.             scroll = 0;
  267.         }
  268.         break;
  269.     case KEY_LEFT:
  270.         if (input_x > 0) input_x--;
  271.         break;
  272.     case KEY_RIGHT:
  273.         if (input_x < curlen) input_x++;
  274.         break;
  275.     case KEY_BACKSPACE:
  276.     case DEL:
  277.         if (input_x > 0) {
  278.             int i;
  279.             input_x--;
  280.             for (i=input_x; i<curlen; i++) instr[i] = instr[i+1];
  281.             mustdraw = 1;
  282.         }
  283.         break;
  284.     case 4:        /* ^d like Emacs */
  285.     case KEY_DC:
  286.         if (input_x < curlen){
  287.             int i;
  288.             for (i=input_x; i<curlen; i++) instr[i] = instr[i+1];
  289.             mustdraw = 1;
  290.         }
  291.         break;
  292.     default:
  293.         if (isprint(key)) {
  294.             if (input_x < MAX_LEN) {
  295.                 wattrset(dialog, inputbox_attr);
  296.                 if (input_x < curlen){
  297.                     int i;
  298.                     for (i=curlen+1; i>input_x; i--) instr[i] = instr[i-1];
  299.                     mustdraw = 1;
  300.                 }
  301.                 instr[input_x] = key;
  302.                 instr[curlen+1] = '\0';
  303.                 input_x++;
  304.                 if (input_x - scroll == box_width) {
  305.                     scroll++;
  306.                 }else{
  307.                     wmove(dialog, box_y, input_x-1 + box_x);
  308.                     if (!password_mode) waddch(dialog, key);
  309.                 }
  310.             }
  311.         }
  312.     }
  313.     if (input_x < scroll){
  314.         scroll--;
  315.     }else if (input_x - scroll == box_width){
  316.         scroll++;
  317.     }
  318.     x.input = input_x;
  319.     x.scroll = scroll;
  320.     if (mustdraw || last_scroll != scroll) drawtxt (dialog);
  321. }
  322.  
  323.  
  324. PUBLIC FIELD_SSTRING::FIELD_SSTRING(
  325.     const char *_prompt,
  326.     SSTRING &_str)
  327.     : FIELD_STRING_BASE (_prompt, _str.get(), _str.getmaxsiz()), str(_str)
  328. {
  329.     backup.setfrom (_str);
  330. }
  331.  
  332. PUBLIC void FIELD_SSTRING::save()
  333. {
  334.     str.setfrom(buf);
  335. }
  336.  
  337. PUBLIC void FIELD_SSTRING::restore ()
  338. {
  339.     str.setfrom(backup);
  340. }
  341. /*
  342.     Draw the field with the prompt
  343. */
  344. PUBLIC void FIELD_SSTRING::html_draw(int nof)
  345. {
  346.     char key[100];
  347.     format_htmlkey (key,nof);
  348.     html_printf ("<tr><td>%s<td>",prompt);
  349.     html_defvar (password_mode ? "password" : "text",key,buf
  350.         ,"size=30 maxlength=256");
  351.     html_defvarcur (key,backup.get());
  352. }
  353.  
  354. /*
  355.     Add a string field to the dialog.
  356. */
  357. PUBLIC FIELD_STRING *DIALOG::newf_str(
  358.     const char *prompt,
  359.     char *str,
  360.     int maxsiz)
  361. {
  362.     FIELD_STRING *s = new FIELD_STRING(prompt,str,maxsiz);
  363.     add (s);
  364.     return s;
  365. }
  366.  
  367. /*
  368.     Add a SSTRING field to the dialog.
  369. */
  370. PUBLIC FIELD_SSTRING *DIALOG::newf_str(
  371.     const char *prompt,
  372.     SSTRING &str)
  373. {
  374.     FIELD_SSTRING *s = new FIELD_SSTRING(prompt,str);
  375.     add (s);
  376.     return s;
  377. }
  378. /*
  379.     Add a password field to the dialog.
  380. */
  381. PUBLIC FIELD_PASSWORD *DIALOG::newf_pass(
  382.     const char *prompt,
  383.     SSTRING &str)
  384. {
  385.     FIELD_PASSWORD *s = new FIELD_PASSWORD(prompt,str);
  386.     add (s);
  387.     return s;
  388. }
  389.